package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.CommentDao;
import com.gdit.zax.entitys.CommentAll;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.webUtil;

public class MoreComment extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int cid = TextUtil.String2int(request.getParameter("cid"), -1);
		int page = TextUtil.String2int(request.getParameter("page"), 1);
		
		//检测参数
		if (cid < 0) {
			O.printFail(response, 1, "请检测cid参数!");
			return;
		}
		if (page < 0) {
			O.printFail(response, 1, "请检测page参数!");
			return;
		}
		
		//检测连接、用户
		Connection conn = DBUtil.getConnection();
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		
		//开始
		DAOFactory dao = DAOFactory.init(conn, request, response);
		CommentDao cdao = dao.getCommentDao();
		ArrayList<CommentAll> list = cdao.listCommentSub(cid, page, 100000);
		ArrayList<HashMap<String, Object>> out = new ArrayList<HashMap<String,Object>>();
		for (CommentAll commentAll : list) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("addtime", commentAll.getT_comment_addtime());
			map.put("content", commentAll.getT_comment_content());
			map.put("courseid", commentAll.getT_comment_courseid());
			map.put("id", commentAll.getT_comment_id());
			map.put("recommentid", commentAll.getT_comment_recommentid());
			map.put("sectionid", commentAll.getT_comment_sectionid());
			map.put("touserid", commentAll.getT_comment_touserid());
			map.put("tousername", commentAll.getT_comment_tousername());
			map.put("username", commentAll.getT_user_nick());
			map.put("videoid", commentAll.getT_video_id());
			map.put("userid", commentAll.getT_user_id());
			map.put("head", webUtil.getWebAbsoluteUrl(commentAll.getT_user_userhead(), Constant.PIC_userhead));
			out.add(map);
		}
		
		O.printSuccess(response, out);
	}

}
